"""Tool for the Bing search API."""

from typing import Dict, List, Literal, Optional, Tuple

from baidusearch.baidusearch import search
from langchain_core.callbacks import CallbackManagerForToolRun
from langchain_core.tools import BaseTool

from langchain_community.utilities.bing_search import BingSearchAPIWrapper


class BaiduSearch(BaseTool):  # type: ignore[override]
    """Tool that queries the Bing search API."""

    name: str = "baidu_search"
    description: str = (
        """
        百度搜索的包装,
        当你需要回答有关时事、基础或权威性的的问题时，
        请使用这个工具，输入应该是搜索查询。
        """
    )

    def _run(
        self,
        query: str,
        run_manager: Optional[CallbackManagerForToolRun] = None,
    ) -> str:
        """Use the tool."""
        return search(query)


class BaiduSearchResults(BaseTool):  # type: ignore[override, override]

    name: str = "baidu_search_results_json"
    description: str = (
        """
        百度搜索的包装,
        当你需要回答有关时事、基础或权威性的的问题时，
        请使用这个工具，输入应该是搜索查询。
        """
    )
    num_results: int = 4
    """Max search results to return, default is 4."""

    def _run(
        self,
        query: str,
        run_manager: Optional[CallbackManagerForToolRun] = None,
    ) -> Tuple[str, List[Dict]]:
        """Use the tool."""
        try:
            results = search(query, self.num_results)
            return str(results), results
        except Exception as e:
            return repr(e), []
